<template>
  <div class="contanier">
    <div class="header"></div>
    <div class="bag">
      <div class="center">
        <div class="avatr">
          <img src="https://img1.baidu.com/it/u=4256350010,1509363888&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400" />
        </div>
        <div class="mes">
          <div>{{ useStore.username }}</div>
        </div>
        <div class="tab">
          <el-tabs v-model="activeName" class="demo-tabs">
            <el-tab-pane label="我的旅游攻略" name="first">
              <div style="margin-top: 20px" v-if="mydata.length">
                <div class="boxs" v-for="(item, index) in mydata" :key="index" @click="goclick(item)">
                  <div class="top">
                    <img :src="item.img" />
                  </div>
                  <div class="choose">
                    <div @click.stop="edit(item)"><el-icon color="#FDD692">
                        <Edit />
                      </el-icon></div>
                    <div @click.stop="deletes(item)"><el-icon color="#FDD692">
                        <Delete />
                      </el-icon></div>
                  </div>
                  <div class="mess">
                    <div class="title">
                      {{ item.title }}
                    </div>
                    <div class="midd">
                      <el-icon size="25px">
                        <StarFilled />
                      </el-icon>
                      {{ (item.collect as collecttype[]).length }}&nbsp;&nbsp;&nbsp;&nbsp;{{ item.date }} &nbsp;&nbsp;&nbsp;&nbsp;平均喜爱度：{{
                        item.averageLikeValue }}
                    </div>
                    <div class="con">
                      {{ item.description }}
                    </div>
                  </div>
                </div>
              </div>
              <el-empty description="暂无个人攻略" v-else />
            </el-tab-pane>
            <el-tab-pane label="我的收藏" name="second">
              <div style="margin-top: 20px" v-if="mycollect.length">
                <div class="box" v-for="(item, index) in mycollect" :key="index">
                  <div class="right">
                    <img :src="item.img" />
                  </div>
                  <div class="left">
                    <el-link class="one" href="" target="_blank" @click="goclick(item)">{{
                      item.title
                    }}</el-link>

                    <div class="two">{{ item.description }}</div>
                    <div class="buttom">
                      <div class="author">
                        By
                        <img
                          src="https://img1.baidu.com/it/u=4256350010,1509363888&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400" />
                        {{ item.author }}
                      </div>
                      <div class="like">喜爱度：{{ item.averageLikeValue }}</div>
                    </div>
                  </div>
                </div>
              </div>
              <el-empty description="暂无个人收藏" v-else />
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
    <LeaveWord class="word"></LeaveWord>
  </div>
</template>

<script setup lang="ts">
import { usestrategyStore } from '../..//stores/strategy'
import type { reqtype, strategyRespnseData,strategyObject ,collecttype} from '@/api/strategy/type.ts'

import { getstrategydata, deletestrategydata } from '../../api/strategy/index'
import { useUserStore } from '../../stores/user'
import LeaveWord from '../../components/Leaveword/index.vue'
let useStore = useUserStore()
import { ref, onMounted } from 'vue'
const strategyStore = usestrategyStore()


const mydata = ref<strategyObject[]>([])
const mycollect = ref<strategyObject[]>([])
const activeName = ref('first')
// 在组件挂载后发起请求
onMounted(() => {
  initdata()
})

const showHtml = (str: string) => {
  if (str.indexOf('\\t') !== -1) {
    console.log('String contains a tab character.')
  }

  return str.replace(/\\t/g, '\u3000\u3000').replace(/\\\t/g, '\u3000\u3000')
}
const edit = (item: any) => {
  const encodedObject = encodeURIComponent(JSON.stringify(item))

  let routerData = $router.resolve({
    path: '/wstrategy',
    query: {
      item: encodedObject
    }
  })
  window.open(routerData.href, '_blank')
}
import { ElMessage, ElMessageBox } from 'element-plus'
const deletes = (item: any) => {
  ElMessageBox.confirm(
    '确认删除该游记吗？',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      let result: reqtype = await deletestrategydata(item.id)
      if (result.status == 0) {
        setInterval(() => {
          ElMessage({
            type: 'success',
            message: '删除成功！',
          })
          window.location.reload();
        }, 2000);
      } else {
        ElMessage({
          type: 'error',
          message: '删除失败！',
        })
      }

    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
const initdata = async () => {
  let result: strategyRespnseData = await getstrategydata()

  if (result.status == 0) {
    for (let i = 0; i < result.data.length; i++) {
      result.data[i].like = JSON.parse(result.data[i].like as string)
      result.data[i].tag = JSON.parse(result.data[i].tag)
      result.data[i].collect = JSON.parse(result.data[i].collect as string)
      result.data[i].content = showHtml(result.data[i].content)
      let description = result.data[i].content
      description = description
        .replace(/(\n)/g, '')
        .replace(/(\t)/g, '')
        .replace(/(\r)/g, '')
        .replace(/<\/?[^>]*>/g, '')
        .replace(/\s*/g, '')
      result.data[i].description = description
    }

    // 遍历数组A中的每个对象B
    result.data.forEach((item) => {
      // 初始化变量来保存value的总和
      let sum = 0
      // 初始化变量来保存like数组的长度
      let length = 0

      // 遍历对象B中的like数组
      if (Array.isArray(item.like)) {
        item.like.forEach((likeItem:any ) => {
          // 累加value的值
          sum += likeItem.value;
          // 增加like数组的长度
          length++;
        });
      }

      // 检查like数组是否有元素，以避免除以0的错误
      if (length > 0) {
        // 计算平均值并保存在对象B中
        item.averageLikeValue = sum / length
      } else {
        item.averageLikeValue = 0
      }

      if (item.author == useStore.username) {
        mydata.value.push(item)
      }
      if (Array.isArray(item.collect)) {
        item.collect.forEach((it) => {
          if (it.username == useStore.username) {
            mycollect.value.push(item)
          }
        })
      }
    })
    strategyStore.modifystrategydatadata(result.data)
  }
}
import { useRouter } from 'vue-router'
let $router = useRouter()
const goclick = (item: strategyObject) => {
  const encodedObject = encodeURIComponent(JSON.stringify(item))

  let routerData = $router.resolve({
    path: '/astrategy',
    query: {
      item: encodedObject
    }
  })
  window.open(routerData.href, '_blank')
}
</script>

<style scoped>
.contaniner {
  position: relative;
}

.word {
  position: absolute;
  left: 6%;
  top: 48%;
  width: 28%;
}

.header {
  width: 100%;
  height: 260px;
  background: url(../../assets/mybg.jpg) no-repeat center bottom;
  position: relative;
}

.bag {
  position: relative;
  left: 0px;
  top: 0px;
  height: 58px;
  background-color: rgba(255, 255, 255, 0.95);
  border-bottom: #d6d6d6 1px solid;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.12);
}

.center {
  width: 980px;
  margin: 0 auto;
  position: relative;

  .avatr {
    position: absolute;
    left: 30px;
    top: -80px;

    img {
      width: 120px;
      height: 120px;
      border-radius: 50%;
    }
  }

  .mes {
    position: absolute;
    margin-top: -40px;
    margin-left: 165px;
    color: white;
    font-size: 28px;
  }

  .tab {
    margin-left: 30%;

    .demo-tabs {}

    .boxs {
      position: relative;

      .top {

        width: 100%;
        height: 300px;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .choose {
        position: absolute;
        top: 53%;
        right: 10%;
        display: flex;
        font-size: 30px;

        div {
          width: 50px;
          height: 50px;
          border-radius: 50%;
          background-color: white;
          margin: 0 20px;
          line-height: 50px;
          text-align: center;
        }
      }

      .mess {
        margin: 10px;

        .title {
          font-size: 23px;
          font-weight: 800;
        }
      }

      .midd {
        display: flex;
        align-items: center;
        margin-top: 10px;
        color: #999;
      }

      .con {
        margin: 20px 0px;
        color: #666;
        font-size: 14px;
        width: 100%;
        height: 60px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        /* 指定显示的行数 */
        -webkit-box-orient: vertical;
        text-overflow: ellipsis;
        line-height: 1.5;
      }
    }

    .box {
      display: flex;
      width: 100%;

      border-radius: 15px;
      margin-bottom: 50px;

      .left {
        width: 65%;
        height: 100%;

        .one {
          font-size: 25px;
        }

        .two {
          margin: 20px 0px;
          color: #666;
          font-size: 14px;
          width: 100%;
          height: 60px;
          overflow: hidden;
          display: -webkit-box;
          -webkit-line-clamp: 3;
          /* 指定显示的行数 */
          -webkit-box-orient: vertical;
          text-overflow: ellipsis;
          line-height: 1.5;
        }
      }

      .right {
        margin-left: 20px;
        margin-right: 20px;

        img {
          width: 170px;
          height: 150px;
        }
      }

      .buttom {
        display: flex;
        color: #ffc952;
        align-content: center;
        justify-content: space-between;

        .author {
          display: flex;
        }

        .like {
          font-size: 15px;
        }

        img {
          width: 20px;
          height: 20px;
          border-radius: 50%;
          margin: 0 5px;
        }
      }
    }
  }
}

div/deep/.el-tabs__item {
  font-size: 20px !important;
  height: 50px;
  color: #000000 !important;
  margin-top: 9px;
}

div/deep/.el-tabs__item.is-active {
  color: #ffc952 !important;
  font-size: 20px;
}

div/deep/.el-tabs__active-bar {
  background-color: #ffc952 !important;
}

div/deep/.el-calendar__body {
  padding: 0;
}

div/deep/ .el-calendar-table .el-calendar-day {
  height: 30px;
  width: 35px;
}
</style>
